import type { MessageOptions, messageType, MessageHandler } from "element-plus";

type OmitTypeMsgOpts = Omit<MessageOptions, "type">;
let elMessageHandler: MessageHandler | null;

export class ClMessage {
  #Message(type: messageType, options: OmitTypeMsgOpts | string) {
    /** 避免多个消息提示 */
    elMessageHandler?.close && elMessageHandler.close();
    if (typeof options === "string") {
      elMessageHandler = ElMessage({ type, message: options });
    } else {
      elMessageHandler = ElMessage({ type, ...options });
    }
  }

  public success(options: OmitTypeMsgOpts | string) {
    this.#Message("success", options);
  }
  public warning(options: OmitTypeMsgOpts | string) {
    this.#Message("warning", options);
  }

  public info(options: OmitTypeMsgOpts | string) {
    this.#Message("info", options);
  }

  public error(options: OmitTypeMsgOpts | string) {
    this.#Message("error", options);
  }

  public closeAll() {
    ElMessage.closeAll();
  }
}
